﻿using ETA.DomainEnum;
using ETA.Entity;
using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ETA.Service
{
    public class AskForHelpService : IAskForHelpService
    {

        #region 属性

        private IAskForHelpRepository _IAskForHelpRepository { set; get; }


        #endregion

        #region 构造函数

        public AskForHelpService(IAskForHelpRepository IAskForHelpRepository)
        {
            _IAskForHelpRepository = IAskForHelpRepository;
        }

        #endregion


        public QueryAskForHelpResponse QueryAskForHelpList(QueryAskForHelpRequest req, out int count)
        {
            count = 0;
            var response = new QueryAskForHelpResponse();
            try
            {
                var express = QueryAskForHelpExpress(req);
                var list = _IAskForHelpRepository.GetAllList(express);

                if (!string.IsNullOrEmpty(req.RolerId))
                {
                    list = list.FindAll(l => l.SubmitUser.Rolers.Any(r => r.RolerId == req.RolerId));
                }

                if (!string.IsNullOrEmpty(req.Type))
                {
                    list = list.FindAll(l => l.AskForHelpTypeEnum == (AskForHelpTypeEnum)(Int32.Parse(req.Type)));
                }

                if (!string.IsNullOrEmpty(req.Category))
                {
                    list = list.FindAll(l => l.AskForHelpKindEnum == (AskForHelpKindEnum)(Int32.Parse(req.Category)));
                }

                if (!string.IsNullOrEmpty(req.Sosid))
                {
                    list = list.FindAll(l => l.AskForHelpId == req.Sosid);
                }
                count = list.Count;
                response.AskForHelpList = list.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList();
                response.Result.IsSuccess = true;

            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
                response.Result.IsSuccess = false;
            }
            return response;
        }

        private Expression<Func<AskForHelp, bool>> QueryAskForHelpExpress(QueryAskForHelpRequest request)
        {
            Expression<Func<AskForHelp, bool>> express = help => true;//这边取isDelete时失败
            if (request.UserId != null)
            {
                express = help => help.SubmitUser.UserId == request.UserId;
            }
            return express;
        }

        public CreateAskForHelpResponse CreateAskForHelp(CreateAskForHelpRequest req)
        {
            var res = new CreateAskForHelpResponse();
            try
            {
                if (req != null)
                {
                    req.AskForHelp.AskForHelpId = _IAskForHelpRepository.GenerateNo("S", 4);
                    res.AskForHelp = _IAskForHelpRepository.Merge(req.AskForHelp);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }

        public ModifyAskForHelpResponse ModifyAskForHelp(ModifyAskForHelpRequest req)
        {
            var res = new ModifyAskForHelpResponse();
            try
            {
                if (req != null)
                {
                    _IAskForHelpRepository.Update(req.AskForHelp);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }


        public Response CommentAskForHelp(string id, DomainEnum.EvaluateEnum evaluate)
        {
            var res = new Response();
            try
            {
                var model = _IAskForHelpRepository.Get(id);
                if (model != null)
                {
                    model.EvaluateEnum = evaluate;
                    _IAskForHelpRepository.Update(model);
                    res.Result.IsSuccess = true;
                }
                else
                {
                    res.Result.ErrMessage = "援助数据id不存在";
                }
            }
            catch (Exception e)
            {
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }

        public Response HandlerAskForHelp(string id, DomainEnum.StateEnum state)
        {
            var res = new Response();
            try
            {
                var model = _IAskForHelpRepository.Get(id);
                if (model != null)
                {
                    model.StateEnum = state;
                    _IAskForHelpRepository.Update(model);
                    res.Result.IsSuccess = true;
                }
                else
                {
                    res.Result.ErrMessage = "援助数据id不存在";
                }
            }
            catch (Exception e)
            {
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }
    }
}
